package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Discussion;
import com.production.CoursePlatform.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/discussion")
public class DiscussionController {
    //此接口已检验
    @Autowired
    private DiscussionService discussionService;

    //创建帖子
    @PostMapping("/createWithoutDid")
    public ResponseEntity<String> createDiscussion(@RequestBody Discussion discussionPost) {
        System.out.println(discussionPost);
        int result = discussionService.createDiscussionPost(discussionPost);
        if (result > 0) {
            return ResponseEntity.ok("帖子创建成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("帖子创建失败");
        }
    }

    //获取帖子通过ct_id
    @GetMapping("/getBYctID/{ctID}")
    public ResponseEntity<List<Discussion>> getByCtID(@PathVariable int ctID) {
        List<Discussion> discussions = discussionService.getListBYctID(ctID);
        return ResponseEntity.ok(discussions);
    }

    // 增加d_likes
    @PostMapping("/incrementLikes/{dID}")
    public ResponseEntity<String> incrementLikes(@PathVariable int dID) {
        discussionService.incrementLikes(dID);
        return ResponseEntity.ok("Likes incremented");
    }

    // 增加d_comments
    @PostMapping("/incrementComments/{dID}")
    public ResponseEntity<String> incrementComments(@PathVariable int dID) {
        discussionService.incrementComments(dID);
        return ResponseEntity.ok("Comments incremented");
    }

    // 增加d_favorites
    @PostMapping("/incrementFavorites/{dID}")
    public ResponseEntity<String> incrementFavorites(@PathVariable int dID) {
        discussionService.incrementFavorites(dID);
        return ResponseEntity.ok("Favorites incremented");
    }

    // 删除帖子
    @DeleteMapping("/delete/{dID}")
    public ResponseEntity<String> deletePost(@PathVariable int dID) {
        discussionService.deletePost(dID);
        return ResponseEntity.ok("Post deleted successfully");
    }
}
